from bs4 import BeautifulSoup

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story" id='one'>Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""


# 1.转类型
soup = BeautifulSoup(html_doc, 'lxml')

# 2.通用解析方法
# 2.1 find   ---  返回符合查询条件的  第一个标签
result_1 = soup.find(name='p')
result_2 = soup.find(attrs={"class":"title"})
result_3 = soup.find(text='Lacie')

result_4 = soup.find(
    name='p',
    attrs={"class":"story"}
)

# 2.2 find_all   ---  返回的是list(标签对象)
result_5 = soup.find_all('a')
result_5 = soup.find_all('a', limit=1)
result_5 = soup.find_all(attrs={"class":"sister"})

# 2.3 select_one   --- css选择器
result_6 = soup.select_one('.sister')  # 类选择器

# 2.4 select
result_6 = soup.select('.sister')
result_6 = soup.select('#one')   #  id选择器

# 后代选择器
result_6 = soup.select('head title')

# 族选择器
result_6 = soup.select('title,.title')

# 属性选择器
result_7 = soup.select('a[id="link3"]')

# 取标签里面包裹的内容
result_8 = soup.select('p')[0].get_text()

# 去标签的属性
result_9 = soup.select('#link1')[0].get('href')



print(result_9)

